package io.lkt.common.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import io.lkt.common.dto.ArticleInfoTemp;
import io.lkt.common.dto.Base;
import io.lkt.common.dto.SysConstant;
//import io.lkt.common.utils.AliyunOssClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 微信文章工具类
 *
 * @version : V1.0
 * @ClassName: WebSpiderUtil
 * @Description: TODO
 * @Auther: chujiu
 * @Date: 2021/2/26 15:59
 */
@Slf4j
//@RestController
public class WebSpiderThreadUtil {
    private static final String ArticleUrl = "https://mp.weixin.qq.com/s/KO7Z4W5e4rKQTXupa0IJIg";
    private static final String VideoUrl = "https://mp.weixin.qq.com/s/YA5kvH0fTmoYb2qhtmgXgA";
    private static final String AudioUrl = "https://mp.weixin.qq.com/s/SVnN6ko712m7BEjNcgOCKQ";

//    public final static Map<String, String> map = new ConcurrentHashMap<>();

    public static void main(String[] args) throws IOException {

        String weiXinPageUrl = "https://mp.weixin.qq.com/s/O8Crvaqf5TwtIAdH8Z7u4w";
//        weiXinPageUrl = ArticleUrl;
//        weiXinPageUrl = VideoUrl;
//        weiXinPageUrl = AudioUrl;

        getWeiXinPage(weiXinPageUrl);
    }

    /**
     * 方法描述：微信公众号文章
     *
     * @param weiXinPageUrl
     * @throws IOException
     */
    public static ArticleInfoTemp getWeiXinPage(String weiXinPageUrl) {
        if (!StringUtils.isNotBlank(weiXinPageUrl) || !weiXinPageUrl.contains("mp.weixin.qq.com")) {
            return null;
        }

        Map<String, String> map = new ConcurrentHashMap<>();
        weiXinPageUrl = weiXinPageUrl.trim().replaceAll("\"", "");

        String webContent = getPageString(weiXinPageUrl);
        //替换微信img
        webContent = replaceImgSrcFromDataSrc(webContent, map);
        webContent = replaceAudioFromMpvoice(webContent, map);

        Document document = Jsoup.parseBodyFragment(webContent);


        Element ele = document.getElementById("js_content");//当根据id取单个元素的时候
        if (ele == null) {
            return null;
        }

        ArticleInfoTemp bArticleInfo = new ArticleInfoTemp();

        String title = document.select("meta[property=\"og:title\"]").attr("content");
        bArticleInfo.setArticleTitle(title);

        String desc = document.select("meta[property=\"og:description\"]").attr("content");
        bArticleInfo.setArticleDesc(desc);

        String image = document.select("meta[property=\"og:image\"]").attr("content");
        String imgUrl = getImg(image, map);
        bArticleInfo.setArticleImgUrl(imgUrl);

        Elements elev = document.getElementsByClass("rich_media_content");//当根据类名之类,取多个元素的时候
        if (elev.isEmpty()) { //eles类似于集合,所以用权isEmpty来判断
            //提取视频元素
            String reg = "url: 'http://mpvideo.qpic.cn/";
            if (document.toString().contains(reg)) {
                Document doc = Jsoup.parseBodyFragment("<div id=\"js_content\"><p id=\"js_common_share_desc\">JS_COMMON_SHARE_DESC</p></div>".replace("JS_COMMON_SHARE_DESC", desc));
                doc.attr("style", "max-width: 100%;");

                String videoName = getVideo(weiXinVideoPage(document), map);
                doc.prepend("<video controls=\"controls\" width=\"800\" height=\"\"> <source src='http://www.viedo.com.cn/movie.mp4' type=\"video/mp4\" /> </video>"
                        .replace("http://www.viedo.com.cn/movie.mp4", videoName));//在div前添加html内容

                bArticleInfo.setArticleContent(doc.toString());
            }
            yesThreadMap_MicroService(map);
            return bArticleInfo;
        }

        Document htmlDoc = Jsoup.parse(webContent);
        //获取替换后的 iframe
        htmlDoc = weiXinVideoPageIframe(htmlDoc, map, weiXinPageUrl);

        //获取替换后的div
        Element headDiv = htmlDoc.select("div#js_content").first();
        headDiv.attr("style", "max-width: 100%;");

        bArticleInfo.setArticleContent(headDiv.toString());

        yesThreadMap_MicroService(map);
        return bArticleInfo;
    }

    /**
     * 方法描述：微信公众号文章(早知天下事)
     *
     * @param weiXinPageUrl
     * @throws IOException
     */
    public static ArticleInfoTemp getWeiXinPage2(String weiXinPageUrl) {
        if (!StringUtils.isNotBlank(weiXinPageUrl) || !weiXinPageUrl.contains("mp.weixin.qq.com")) {
            return null;
        }

        Map<String, String> map = new ConcurrentHashMap<>();
        weiXinPageUrl = weiXinPageUrl.trim().replaceAll("\"", "");

        String webContent = getPageString(weiXinPageUrl);
        //替换微信img
        webContent = replaceImgSrcFromDataSrc(webContent, map);
        webContent = replaceAudioFromMpvoice(webContent, map);

        Document document = Jsoup.parseBodyFragment(webContent);

        Element ele = document.getElementById("js_content");//当根据id取单个元素的时候
        if (ele == null) {
            return null;
        }

        ArticleInfoTemp bArticleInfo = new ArticleInfoTemp();

        String title = document.select("meta[property=\"og:title\"]").attr("content");
        bArticleInfo.setArticleTitle(title);

        String desc = document.select("meta[property=\"og:description\"]").attr("content");
        bArticleInfo.setArticleDesc(desc);

//        String image = document.select("meta[property=\"og:image\"]").attr("content");
//        String imgUrl = getImg(image, map);
//        bArticleInfo.setArticleImgUrl(imgUrl);

        Elements elev = document.getElementsByClass("rich_media_content");//当根据类名之类,取多个元素的时候
        if (elev.isEmpty()) { //eles类似于集合,所以用权isEmpty来判断
            //提取视频元素
            String reg = "url: 'http://mpvideo.qpic.cn/";
            if (document.toString().contains(reg)) {
                Document doc = Jsoup.parseBodyFragment("<div id=\"js_content\"><p id=\"js_common_share_desc\">JS_COMMON_SHARE_DESC</p></div>".replace("JS_COMMON_SHARE_DESC", desc));
                doc.attr("style", "max-width: 100%;");

                String videoName = getVideo(weiXinVideoPage(document), map);
                doc.prepend("<video controls=\"controls\" width=\"800\" height=\"\"> <source src='http://www.viedo.com.cn/movie.mp4' type=\"video/mp4\" /> </video>"
                        .replace("http://www.viedo.com.cn/movie.mp4", videoName));//在div前添加html内容

                bArticleInfo.setArticleContent(doc.toString());
            }
            yesThreadMap_MicroService(map);
            return bArticleInfo;
        }

        Document htmlDoc = Jsoup.parse(webContent);
        //获取替换后的 iframe
        htmlDoc = weiXinVideoPageIframe(htmlDoc, map, weiXinPageUrl);

        //获取替换后的div
        Element headDiv = htmlDoc.select("div#js_content").first();
        headDiv.attr("style", "max-width: 100%;");

//        headDiv.getElementById("activity-name").remove();// 移除标题
//        headDiv.getElementById("meta_content").remove();// 移除子标题
//        headDiv.getElementsByClass("qr_code_pc").remove();// 移除微信扫一扫
        // 移除文首段落
        for (int i = 0; i < 4; i++) {
            headDiv.getElementsByTag("p").first().remove();
        }

        // 移除文末段落
        for (int i = 0; i < 7; i++) {
            headDiv.getElementsByTag("p").last().remove();
        }

        bArticleInfo.setArticleContent(headDiv.toString());

        yesThreadMap_MicroService(map);
        return bArticleInfo;
    }


    //多线程传输
    public static void yesThreadMap(Map<String, String> map) {
        long beginTime = System.currentTimeMillis();
        System.out.println("开始：" + beginTime);
        // 创建容量为 6 缓冲池
        // ExecutorService cachedThreadPool = Executors.newFixedThreadPool(6);
        // 核心线程满了，则进入队列，队列满了，则创建新线程，当线程数达到最大线程数，则进入拒绝策略
        ThreadPoolExecutor cachedThreadPool = new ThreadPoolExecutor(16, 20, 1,
                TimeUnit.MINUTES, new LinkedBlockingDeque<>(), new NamedThreadFactory("测试"));
        System.out.println(map);
        for (Map.Entry<String, String> info : map.entrySet()) {
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {

                    AliyunOssClientUtil.uploadFileByUrl(info.getKey(), info.getValue());
                    System.out.println("多线-" + Thread.currentThread().getName()+"_完成"+System.currentTimeMillis());//12583
                }
            });

        }

        // 关闭线程池
        cachedThreadPool.shutdown();

        // 轮询线程池的任务是否都执行完
        while (true) {
            if (cachedThreadPool.isTerminated()) {
                long time2 = System.currentTimeMillis() - beginTime;
                System.out.println("结束：" + time2);//6954
                break;
            }
        }

    }

    //微服务传输
    public static void yesThreadMap_MicroService(Map<String, String> map) {
        long beginTime = System.currentTimeMillis();
        System.out.println("开始：" + beginTime);

        AtomicReference<Long> sum= new AtomicReference<>(0L);
        for (Map.Entry<String, String> info : map.entrySet()) {
            Long kStart=System.currentTimeMillis();

            Map<String, Object> map1dMap = Maps.newHashMap();
            map1dMap.put("ossKey", info.getKey());
            map1dMap.put("ossValue", info.getValue());
//            String finalReqUrl = "http://172.18.165.241:9000/fileserver/fileOss/updateFile";//网关地址
            String finalReqUrl = "http://120.78.222.19:9000/fileserver/fileOss/updateFile";//网关地址
            HttpClientUtils.sendPost(finalReqUrl, map1dMap);

            Long kEnd=System.currentTimeMillis();
            Long c=kEnd-kStart;
            System.out.println("多线-" + Thread.currentThread().getName() + "_开始："+kStart+"_完成:" + kEnd+"_用时："+c);
            sum.set(sum.get() + c);

        }
        long time2 = System.currentTimeMillis() - beginTime;
        System.out.println("结束：" + time2+"_sum:"+sum.get());//6954

    }

    //单线程传输
    public static void yesThreadMap_Single(Map<String, String> map) {
        long beginTime = System.currentTimeMillis();
        System.out.println("开始：" + beginTime);

        AtomicReference<Long> sum= new AtomicReference<>(0L);
        for (Map.Entry<String, String> info : map.entrySet()) {
            Long kStart=System.currentTimeMillis();
            AliyunOssClientUtil.uploadFileByUrl(info.getKey(), info.getValue());
            Long kEnd=System.currentTimeMillis();
            Long c=kEnd-kStart;
            System.out.println("多线-" + Thread.currentThread().getName() + "_开始："+kStart+"_完成:" + kEnd+"_用时："+c);
            sum.set(sum.get() + c);

        }
        long time2 = System.currentTimeMillis() - beginTime;
        System.out.println("结束：" + time2+"_sum:"+sum.get());//6954

    }

    //多线程传输(不用线程池)
    public static void yesThreadMap_NoTP(Map<String, String> map) {
        long beginTime = System.currentTimeMillis();
        System.out.println("开始：" + beginTime);

        Thread[] threads=new Thread[map.size()];
        CountDownLatch latchn=new CountDownLatch(threads.length);

        int k=0;
        AtomicReference<Long> sum= new AtomicReference<>(0L);
        for (Map.Entry<String, String> info : map.entrySet()) {
            threads[k]=new Thread(()->{
                Long kStart=System.currentTimeMillis();
                AliyunOssClientUtil.uploadFileByUrl(info.getKey(), info.getValue());
                latchn.countDown();
                Long kEnd=System.currentTimeMillis();
                Long c=kEnd-kStart;
                System.out.println("多线-" + Thread.currentThread().getName() + "_开始："+kStart+"_完成:" + kEnd+"_用时："+c);
                sum.set(sum.get() + c);
            });
            k++;
        }

        for(int i=0;i<threads.length;i++){
            threads[i].start();
        }

        try {
            latchn.await();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        long time2 = System.currentTimeMillis() - beginTime;
        System.out.println("结束：" + time2+"_sum:"+sum.get());//6954

    }

    /**
     * 根据微信音频文章网页内容获取音频地址
     *
     * @param document 网页内容
     * @return 音频地址
     */
    private static String weiXinAudioPage(Document document) {
        String uri = "http://res.wx.qq.com/voice/getvoice?mediaid=MEDIAID";
        String mediaId = document.select("mpvoice[data-pluginname=\"insertaudio\"]").attr("voice_encode_fileid");
        uri = uri.replace("MEDIAID", mediaId);

        return uri;
    }

    /**
     * 根据微信音频文章网页内容获取视频地址
     * //mp.weixin.qq.com/mp/readtemplate?t=pages/video_player_tmpl&auto=0&vid=wxv_1757392981919121413
     *
     * @param document 网页内容
     * @return 视频地址
     */
    /**
     * 接口一：视频播放的 iframe 原地址
     * 请求方式：GET
     * 参数：vid：微信视频ID
     * http://mp.weixin.qq.com/mp/readtemplate?t=pages/video_player_tmpl&auto=0&vid=wxv_1219759656583774208
     * <p>
     * 接口二：请求视频接口,获取视频 url
     * 请求方式：POST
     * 参数：vid：微信视频ID
     * http://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=wxv_1219759656583774208&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json
     *
     * @param document
     * @return
     */
    private static String weiXinVideoPage(Document document) {
        String webContent = document.toString();
        //你想要的内容左右所对应,有空格的注意空格
        String path = "url: 'http://mpvideo.qpic.cn/";

        String vid = "";
        if (webContent.contains("wxv_")) {
            String content = webContent;
            content = content.substring(content.indexOf("wxv_")).substring(0, 30);//截取之后的字符串

            content = content.substring(0, content.indexOf("'"));//截取之前的字符串

            vid = content;
        }

        String web = "";
        if (webContent.contains(path)) {
            String content = webContent;
            content = content.substring(content.indexOf("url: 'http://mpvideo.qpic.cn/")).replace("url: '", "");//截取之后的字符串
            content = content.substring(0, content.indexOf("video_quality_level: '3' * 1 || 0,"));//截取之前的字符串
            content = content.substring(0, content.indexOf("',"));

            web = content;
        }

        String format_id = "";
        if (webContent.contains(".mp4")) {
            String content = web;
            content = content.substring(content.indexOf(".f")).substring(0, 30).replace(".f", "");//截取之后的字符串
            content = content.substring(0, content.indexOf(".mp4?"));//截取之前的字符串

            format_id = content;
        }

        String uri = web + "&vid=" + vid + "&format_id=" + format_id;
        uri = uri.replace("\\x26amp;", "&");

        return uri;
    }

    /**
     * 接口一：视频播放的 iframe 原地址
     * 请求方式：GET
     * 参数：vid：微信视频ID
     * http://mp.weixin.qq.com/mp/readtemplate?t=pages/video_player_tmpl&auto=0&vid=wxv_1219759656583774208
     * <p>
     * 接口二：请求视频接口,获取视频 url
     * 请求方式：POST
     * 参数：vid：微信视频ID
     * http://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=wxv_1219759656583774208&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json
     *
     * @param document
     * @return
     */
    private static Document weiXinVideoPageIframe(Document document, Map<String, String> map, String weiXinPageUrl) {
        String url = "http://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=WXV_WID&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json";

        Elements nodes = document.select("iframe");
        int nodeLength = nodes.size();

        int videoIndex = 0;// 计数
        List<String> videoList = new ArrayList<>();
        if (nodeLength > 0) {
            videoList = getDynamicPageVideo(weiXinPageUrl);
            System.out.println("weiXinPageUrl:"+weiXinPageUrl);
            System.out.println("videoList_Size:"+videoList.size());
            System.out.println("[videoList] = " + videoList);
        }

        for (int i = 0; i < nodeLength; i++) {
            Element e = nodes.get(i);

            System.out.println("Element:"+i);
//            String dataSrc = e.attr("data-mpvid");
//
//            if (StringUtils.isNotBlank(dataSrc)) {
//                String vUrl = url.replace("WXV_WID", dataSrc);
//                try {
//                    String body = Jsoup.connect(vUrl).ignoreContentType(true).execute().body().toString();
//
//                    //测试 Fastjson 反序列化
//                    Base base = JSON.parseObject(body, Base.class);
//                    if (base != null && base.getUrlInfo() != null && base.getUrlInfo().size() > 0 && base.getUrlInfo().get(0).getUrl() != null) {
//                        String result = base.getUrlInfo().get(0).getUrl();
//
//                        //获取Video
//                        result = getVideo(result, map);
//
//                        //替换为自己的
//                        e.wrap("<video controls=\"controls\" width=\"800\">" +
//                                "<source src=\"" + result + "\" type=\"video/mp4\"></source>" +
//                                "</video>");
//                    }
//                } catch (IOException ex) {
//
//                }
//            }

            // 替换视频标签
            if (e.hasClass("video_iframe")) {
                // 如果 Jsoup 得到的视频数量和爬虫得到的视频数量一致
                if (nodeLength == videoList.size()) {
                    String src = videoList.get(videoIndex);

                    // 如果是腾讯视频，则采用原地址；否则，上传到 oss 服务器，采用 oss 的路径
                    String ossVideoUrl = src.contains("v.qq.com") ? src : getVideo(src, map);

                    String dataSrc = e.attr("data-src");// 获取视频地址
                    if (dataSrc.contains("v.qq.com")) {
                        // 创建 object 标签
                        Element objectElement = document.createElement("object");
                        objectElement.attr("type", "text/html");
                        objectElement.attr("data", ossVideoUrl);
                        objectElement.attr("style", "width:97vw; height:250px");
                        e.after(objectElement);// 从该元素后面插入新元素
                    } else {
                        // 创建 video 标签
                        Element video = document.createElement("video");
                        video.attr("src", ossVideoUrl);
                        video.attr("controls", "controls");
                        e.after(video);// 从该元素后面插入新元素
                    }

                    e.remove();// 删除元素
                    System.out.println("video:"+videoIndex);
                    videoIndex++;// 递增
                } else {
                    // 如果 Jsoup 得到的视频数量和爬虫得到的视频数量不一致
                    String dataSrc = e.attr("data-src");// 获取视频地址
                    if (dataSrc.contains("v.qq.com")) {
                        // 创建 object 标签
                        Element objectElement = document.createElement("object");
                        objectElement.attr("type", "text/html");
                        objectElement.attr("data", dataSrc);
                        objectElement.attr("style", "width:97vw; height:250px");
                        e.after(objectElement);// 从该元素后面插入新元素
                    } else {
                        String src = videoList.get(videoIndex);

                        // 如果是腾讯视频，则采用原地址；否则，上传到 oss 服务器，采用 oss 的路径
                        String ossVideoUrl = getVideo(src, map);
                        // 创建 video 标签
                        Element video = document.createElement("video");
                        video.attr("src", ossVideoUrl);
                        video.attr("controls", "controls");
                        e.after(video);// 从该元素后面插入新元素

                        System.out.println("video:"+videoIndex);
                        videoIndex++;// 递增
                    }

                    e.remove();// 删除元素
                }
            }
        }
        return document;
    }

    /**
     * 将htmlBody中所有mpvoice标签替换为audio,
     *
     * @param htmlBody
     * @return 返回替换后的内容
     */
    private static String replaceAudioFromMpvoice(String htmlBody, Map<String, String> map) {
        String url = "http://res.wx.qq.com/voice/getvoice?mediaid=MEDIA_ID";

        Document document = Jsoup.parseBodyFragment(htmlBody);

        Elements nodes = document.select("mpvoice");
        int nodeLength = nodes.size();
        for (int i = 0; i < nodeLength; i++) {
            Element e = nodes.get(i);

            String urlID = e.attr("voice_encode_fileid");

            if (StringUtils.isNotBlank(urlID)) {
                String result = url.replace("MEDIA_ID", urlID);
                //替换为自己的
                result = getAudio(result, map);
                e.wrap("<audio  src=\"https://res.wx.qq.com/voice/getvoice\" controls style=\"display: inline\"></audio>".replace("https://res.wx.qq.com/voice/getvoice", result));//在div前添加html内容

            }
        }

        if (htmlBody.contains("<html>")) {
            return document.toString();
        } else {
            return document.select("body>*").toString();
        }
    }

    /**
     * 获取网页
     *
     * @param uri
     * @return
     * @throws IOException
     */
    private static String getPageString(String uri) {
        String dataSrc = uri;
        //获取网页内容
        String jsonStr = "";
        try {
            jsonStr = Jsoup.connect(dataSrc).ignoreContentType(true).execute().body();
        } catch (IOException ex) {

        } finally {
            return jsonStr;
        }
    }

    private static String getImg(String uri, Map<String, String> map) {
        String dataSrc = uri;

        //创建保存路径
        String formatDate = DateUtils.getFormatDate("yyyy-MM-dd", DateUtils.getCurDate());
        String fileName = UUID.randomUUID().toString() + ".png";
        String dirName = SysConstant.OSS_FILE_DIR_PREFIX_WEB_SPIDER_IMG + formatDate + "/";

        //阿里云文件路径
        String webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + dirName + fileName;

        if (map.get(dataSrc) != null) {
            webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + map.get(dataSrc);
        } else {
            map.put(dataSrc, dirName + fileName);
        }

        return webShowFilePath;
    }

    private static String getVideo(String uri, Map<String, String> map) {
        String dataSrc = uri;

        //创建保存路径
        String formatDate = DateUtils.getFormatDate("yyyy-MM-dd", DateUtils.getCurDate());
        String fileName = UUID.randomUUID().toString() + ".mp4";
        String dirName = SysConstant.OSS_FILE_DIR_PREFIX_WEB_SPIDER_VIDEO + formatDate + "/";

        //阿里云文件路径
        String webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + dirName + fileName;

        if (map.get(dataSrc) != null) {
            webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + map.get(dataSrc);
        } else {
            map.put(dataSrc, dirName + fileName);
        }

        return webShowFilePath;
    }

    private static String getAudio(String uri, Map<String, String> map) {
        String dataSrc = uri;

        //创建保存路径
        String formatDate = DateUtils.getFormatDate("yyyy-MM-dd", DateUtils.getCurDate());
        String fileName = UUID.randomUUID().toString() + ".mp3";
        String dirName = SysConstant.OSS_FILE_DIR_PREFIX_WEB_SPIDER_AUDIO + formatDate + "/";

        //阿里云文件路径
        String webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + dirName + fileName;

        if (map.get(dataSrc) != null) {
            webShowFilePath = SysConstant.OSS_ACCESS_PREFIX + map.get(dataSrc);
        } else {
            map.put(dataSrc, dirName + fileName);
        }

        return webShowFilePath;
    }

    /**
     * 将htmlBody中所有img标签中的src内容替换为原data-src的内容,
     * <br/>如果不报含data-src,则src的内容不会被替换
     * <br/>例如:
     * <br/> 替换前 &lt;img data-src="weixinImgUrl" src="localImgUrl" othreAttr="其他属性不变"/&gt;
     * <br/> 替换后 &lt;img src="weixinImgUrl" othreAttr="其他属性不变" /&gt;
     *
     * @param htmlBody
     * @return 返回替换后的内容
     */
    private static String replaceImgSrcFromDataSrc(String htmlBody, Map<String, String> map) {
        Document document = Jsoup.parseBodyFragment(htmlBody);

        Elements nodes = document.select("img");
        int nodeLength = nodes.size();
        for (int i = 0; i < nodeLength; i++) {
            Element e = nodes.get(i);
            String dataSrc = e.attr("data-src");
            if (StringUtils.isNotBlank(dataSrc)) {
                //替换图片为自己的
                e.attr("src", getImg(dataSrc, map));
                e.removeAttr("data-src");
            }
        }

        if (htmlBody.contains("<html>")) {
            return document.toString();
        } else {
            return document.select("body>*").toString();
        }
    }

    /**
     * 根据url生成File
     */
    private static InputStream getFileByUrl(String webUrl, File resultFile) {
//        System.out.println("下载文件的uri：" + webUrl);
        InputStream test = null;

        HttpURLConnection conn = null;
        InputStream inStream = null;
        FileOutputStream outStream = null;
        try {
            URL url = new URL(webUrl);
            //打开链接
            conn = (HttpURLConnection) url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //建立连接的超时时间
            conn.setConnectTimeout(10 * 1000);
            //传递数据的超时时间
            conn.setReadTimeout(10 * 1000);
            //通过输入流获取图片数据
            inStream = conn.getInputStream();
            test = inStream;

            //创建输出流
            outStream = new FileOutputStream(resultFile);
            byte[] buffer = new byte[1024];
            //每次读取的字符串长度，如果为-1，代表全部读取完毕
            int len;
            //使用一个输入流从buffer里把数据读取出来
            while ((len = inStream.read(buffer)) != -1) {
                //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
                outStream.write(buffer, 0, len);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                conn.disconnect();
            }

            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return test;
    }

    /**
     * 获取动态页面的视频链接
     * @param weiXinPageUrl 链接
     * @return
     */
    public static List<String> getDynamicPageVideo(String weiXinPageUrl) {
//        List<String> videoList = new ArrayList<>();
        // Phantomjs 获取页面的视频链接
//        List<String> originalVideoList = PhantomjsUtil.parseVideo(weiXinPageUrl);

        // Chrome 获取页面的视频链接
        List<String> originalVideoList = ChromeUtil.parseVideo(weiXinPageUrl);
        // 压缩
        /*if (CollectionUtils.isNotEmpty(originalVideoList) && originalVideoList.size() > 0) {
            for (String link : originalVideoList) {
                String ossVideoLink = "";
                try {
                    ossVideoLink = compressionAndUploadVideo(link);
                } catch (IOException e) {

                }

                if (ossVideoLink.equals("")) {
                    videoList.add(link);// 使用原来的
                } else {
                    videoList.add(ossVideoLink);// 使用新的地址
                }
            }
        }*/
//        return videoList;
        return originalVideoList;
    }
}
